import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo30 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service= Executors.newFixedThreadPool(4);//代表线程池中有4个线程
        for (int i = 0; i < 100; i++) {
            int id =i;//让id成为事实final,让lambda捕获
            service.submit(()->{
               Thread current = Thread.currentThread();//当前线程
                System.out.println("hello thread"+ id +","+ current.getName());
            });
        }

        //最好不要立即就终止, 可能任务还没执行完,线程就终止了
        Thread.sleep(2000);

        //把线程池里的线程都 终止掉
        service.shutdown();
        System.out.println("程序退出");
    }
}
